#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : day_report_yj_ch_second.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-10-29 14:42 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

from collections import OrderedDict
from report_system.utils import date_util
from report_system.utils import mail_util
from report_system.utils import excel_util
from report_system.utils import mailbox_util
from report_system.report_specify import yj_ch_second_demand_specify_dao as yc
from report_system.utils.log_util import log
from report_system.utils.development_util import debug

if debug:
    receivers = 'dt@iyoujia.com'
    city_mails = mailbox_util.mails_test
else:
    receivers = "shenzhiqiang@iyoujia.com;yitangf@sweetome.com;wangsuxiao@city-home.cn;zhangxian@city-home.cn;" \
                "songweiyun@iyoujia.com;xuzhehang@iyoujia.com;zhuqiang@iyoujia.com;zhengliangl@sweetome.com;" \
                "lantianxiang@city-home.cn;wujiming@iyoujia.com;dt@iyoujia.com;" \
                "fengshuaishuai@city-home.cn;huqi2@city-home.cn;qikai@iyoujia.com;cuijing@city-home.cn"
    city_mails = mailbox_util.mails_all


def get_df_color(dt, have_long, own_type=(1, 2)):
    df_dict = OrderedDict()
    color_dict = OrderedDict()

    district_yesterday_df, district_yesterday_color = yc.process_district_yesterday(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['大区-昨日'] = district_yesterday_df
    color_dict['大区-昨日'] = district_yesterday_color

    city_yesterday_df, city_yesterday_color = yc.process_city_yesterday(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['城市-昨日'] = city_yesterday_df
    color_dict['城市-昨日'] = city_yesterday_color

    work_yesterday_df, work_yesterday_color = yc.process_work_yesterday(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['门店-昨日'] = work_yesterday_df
    color_dict['门店-昨日'] = work_yesterday_color

    house_yesterday_df, house_yesterday_color = yc.process_house_yesterday(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['房屋-昨日'] = house_yesterday_df
    color_dict['房屋-昨日'] = house_yesterday_color

    district_seven_ago_df, district_seven_ago_color = yc.process_district_seven_ago(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['大区-前7日'] = district_seven_ago_df
    color_dict['大区-前7日'] = district_seven_ago_color

    city_seven_ago_df, city_seven_ago_color = yc.process_city_seven_ago(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['城市-前7日'] = city_seven_ago_df
    color_dict['城市-前7日'] = city_seven_ago_color

    work_seven_ago_df, work_seven_ago_color = yc.process_work_seven_ago(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['门店-前7日'] = work_seven_ago_df
    color_dict['门店-前7日'] = work_seven_ago_color

    house_seven_ago_df, house_seven_ago_color = yc.process_house_seven_ago(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['房屋-前7日'] = house_seven_ago_df
    color_dict['房屋-前7日'] = house_seven_ago_color

    district_month_df, district_month_color = yc.process_district_month(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['大区-月累计'] = district_month_df
    color_dict['大区-月累计'] = district_month_color

    city_month_df, city_month_color = yc.process_city_month(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['城市-月累计'] = city_month_df
    color_dict['城市-月累计'] = city_month_color

    work_month_df, work_month_color = yc.process_work_month(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['门店-月累计'] = work_month_df
    color_dict['门店-月累计'] = work_month_color

    house_month_df, house_month_color = yc.process_house_month(dt=dt, have_long=have_long, own_type=own_type)
    df_dict['房屋-月累计'] = house_month_df
    color_dict['房屋-月累计'] = house_month_color

    return df_dict, color_dict


def process(dt, own_type=(1, 2)):
    """
    【不包含】长租
    :param dt:
    :param own_type:
    :return:
    """
    # todo ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓【不包含】长租↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    df_no_long_dict, color_no_long_dict = get_df_color(dt=dt, have_long=False, own_type=own_type)
    # excel_util.pd_to_excel(df_no_long_dict, '000运营数据', engine='xlsxwriter')
    text = """    Dear All:
                附件是【运营数据-【不包含】长租】！
                请查收！
                谢谢！
        如有问题！
        请联系: dt@iyoujia.com
        """

    mail_util.distribute_mail(df_dict=df_no_long_dict, file_name='运营数据-不包含长租', text=text, html_sheet_name='城市-月累计',
                              title="【运营数据-【不包含】长租】", colors_dict=color_no_long_dict, receiver=receivers, na_rep='-',
                              engine='xlsxwriter',
                              city_mails=city_mails, city=True, city_key='城市', uuid='city', freeze_panes=(3, 3))

    # todo ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑【不包含】长租↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


def run(dt=None):
    dt = dt if dt else date_util.curdate()

    try:
        process(dt)
    except Exception as e:
        log.exception(e)
        mail_util.error_mail("运营数据", e.__str__())


if __name__ == '__main__':
    run()
